package com.fractalist.man.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fractalist.base.core.utils.MyDateUtils;
import com.fractalist.man.base.SqlUtils;
import com.fractalist.man.service.IScoreLogService;
import com.fractalist.man.service.IScoreRuleService;
import com.fractalist.man.service.IScoreService;
import com.fractalist.man.service.IUserService;

@Service
public class ScoreService implements IScoreService {
	@Autowired
	private IScoreLogService scoreLogService;
	@Autowired
	private IUserService userService;

	@Override
	public boolean addScore(Long curUserId, BigDecimal addScore,
			Long scoreRuleId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", curUserId);
		Map<String, Object> user = userService.queryOneRow(SqlUtils
				.genderQuerySql("man_base_user",
						SqlUtils.genderConditionsByType(paramMap, "and")));
		if (user != null && user.size() > 0) {
			try {
				String now = MyDateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss");
				userService.update("update man_base_user set score = score + "
						+ addScore + ",update_time = '" + now + "' where id = "
						+ curUserId);
				paramMap = new HashMap<String, Object>();
				paramMap.put("score", addScore);
				paramMap.put("userId", curUserId);
				paramMap.put("scoreRuleId", scoreRuleId);
				paramMap.put("createDate", now);
				scoreLogService.add(SqlUtils.genderInsertSql(
						"man_base_score_log", paramMap));
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
}
